package Pet.Management.controller;

import Pet.Management.domain.entity.PetCareOrder;
import Pet.Management.service.PetCareOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/pet-care/orders")
public class PetCareOrderController {

    @Autowired
    private PetCareOrderService orderService;

    @PostMapping
    public ResponseEntity<Map<String, Object>> createOrder(@RequestBody PetCareOrder order) {
        try {
            PetCareOrder savedOrder = orderService.createOrder(order);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "订单创建成功");
            response.put("data", savedOrder);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "订单创建失败: " + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getOrder(@PathVariable Long id) {
        try {
            PetCareOrder order = orderService.getOrderById(id);
            Map<String, Object> response = new HashMap<>();
            if (order != null) {
                response.put("code", 200);
                response.put("data", order);
            } else {
                response.put("code", 404);
                response.put("msg", "订单不存在");
            }
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "获取订单失败: " + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 